//
//  TPPutDataRequest.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/6/16.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "TPPutDataRequest.h"

@interface TPPutDataRequest()
@property (nonatomic,strong,readwrite)QNUploadManager *manager;
@property (nonatomic,copy,readwrite)NSString *token;

@end
@implementation TPPutDataRequest

+(instancetype)sharePutDataRequest{

    static TPPutDataRequest *putDataRequest;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        putDataRequest = [[TPPutDataRequest alloc]init];
        [putDataRequest setup];
    });
    
    return putDataRequest;
    
}
+(NSString *)getUrl:(NSString *)key{
  
    return [kQiniuUrl stringByAppendingString:key];
}

-(void)setup{
  
//    NSError *error;

    _manager = [[QNUploadManager alloc]initWithConfiguration:nil];
//    
    [self getTokenSuccess:^{
        ;
    } fail:^{
        ;
    }];
}



-(void)getTokenSuccess:(void(^)(void))successBlock fail:(void(^)(void))failBlock{
    
    [[TPRequestClient sharedClient]getUploadTokenSuccess:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        self.token = respone.data[@"token"];
        successBlock();
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        failBlock();
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        failBlock();
    }];
    
    
}

-(void)putFile:(NSString *)file key:(NSString *)key progressHandler:(QNUpProgressHandler)progress complete:(QNUpCompletionHandler)completionHandler{
    
    if(!key)
        key = [NSString stringWithFormat:@"tripvi_%@",[NSDate getTimeIntervalSince]];
    
    void(^hasTokenBlock)(void) = ^(){
         QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:^BOOL{
             return NO;
         }];
        
         [_manager putFile:file key:key token:self.token complete:completionHandler option:uploadOption];
    };
    
    if(self.token){
      
        hasTokenBlock();
        
    }else{
        [self getTokenSuccess:^{
            
            hasTokenBlock();
            
        } fail:^{
            [UIView xw_hideTitle:AppLocalString(@"Seems to be out of the question")];
        }];
    
    }
    
    
   
    
}

-(void)putData:(NSData *)data key:(NSString *)key complete:(QNUpCompletionHandler)completionHandler{
    
    if(!key)
        key = [NSString stringWithFormat:@"tripvi_%@",[NSDate getTimeIntervalSince]];
    
    
    void(^hasTokenBlock)(void) = ^(){
         [_manager putData:data key:key token:self.token complete:completionHandler option:nil];
    };
//    
    if(self.token){
        
        hasTokenBlock();
        
    }else{
        [self getTokenSuccess:^{
            
            hasTokenBlock();
            
        } fail:^{
            [UIView xw_hideTitle:AppLocalString(@"Seems to be out of the question")];
        }];
        
    }
  
}

@end
